# -*- encoding: utf-8 -*-

from src.ApiTest.ApiCase.Database.api_case_database import ApiCase, ApiCaseSchema
from Common.yaml_method import YamlMethod


class ApiCaseList:
    """
    获取接口测试用例列表接口
    """

    @staticmethod
    def api_case_list(page, limit, data_name, project_name):
        """
        获取接口测试用例列表接口
        :param page: 页码
        :param limit: 每页多少条数据
        :param data_name: 用例名称
        :param project_name: 项目名称
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        if data_name == '' and project_name == '':
            data = ApiCase.query.filter().paginate(page=page, per_page=limit)
        elif data_name != '' and project_name == '':
            data = ApiCase.query.filter_by(caseName=data_name).paginate(page=page, per_page=limit)
        elif data_name == '' and project_name != '':
            data = ApiCase.query.filter_by(projectName=project_name).paginate(page=page, per_page=limit)
        else:
            data = ApiCase.query.filter_by(caseName=data_name, projectName=project_name).paginate(page=page, per_page=limit)
        info = []
        for i in data.items:
            api_schema = ApiCaseSchema()
            api_data = api_schema.dump(i)
            # 将单条信息添加到info中
            info.append(api_data)

        total = data.total
        has_more = data.has_next
        res = {
            'code': code[0],
            'message': 'success',
            'data': {
                'apiCaseList': info,
                'total': total,
                'has_more': has_more
            }
        }

        return res
